<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2018/4/14
  Time: 13:53
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>新闻管理</title>
    <link href="${pageContext.request.contextPath}/static/css/bootstrap.min.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/css/plugins/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/css/font-awesome.min93e3.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/css/style.min.css?v=4.1.0" rel="stylesheet">
    <style>
        .admin-so {
            height: 40px;
            line-height: 40px;
            margin-bottom: 10px;
        }

        .admin-so-text a {
            margin-right: 10px;
        }
    </style>
</head>
<body>

<div class="wrapper wrapper-content animated fadeInRight">

    <div class="row">
        <div class="col-md-5" style="margin-bottom: 20px">
            <div class="admin-so"><i class="fa fa-search"></i> 关键字搜索</div>
            <form>
                <div class="input-group">
                    <input type="text" class="form-control" name='keywords' placeholder="请输入新闻标题或内容搜索" value=''>
                    <span class="input-group-btn">
                        <button class="btn btn-primary" type="button" onclick="Searchnews()">搜索</button>
                    </span>
                </div>
            </form>
        </div>
        <div class="col-md-1 col-lg-offset-3">
            <div class="admin-so"><i class="fa fa-calendar"></i> 新增新闻</div>
            <button class="btn btn-success" onclick='AddNews()'>新增新闻</button>
        </div>
        <div class="col-md-1 col-lg-offset-1">
            <div class="admin-so"><i class="fa fa-calendar"></i> 删除新闻</div>
            <button class="btn btn-danger" onclick='Delnews()'>删除新闻</button>
        </div>
    </div>


    <%--<table id="testtable" class="table table-hover table-striped" data-toggle="table" data-height="690"
           data-pagination="true" data-click-to-select="true" data-mobile-responsive="true"
           data-search-on-enter-key="true" data-unique-id="id"
           data-icon-size="outline" data-search="false" data-page-size="15" data-page-list="[ 10,15, 25, 35]">

        <thead>
        <th data-field="state" data-checkbox="true"></th>
        <th data-field="id">新闻ID</th>
        <th>新闻标题</th>
        <th>原文地址</th>
        <th>置顶</th>
        <th>发布时间</th>
        <th>创建时间</th>
        <th>操作</th>
        </thead>
        <tbody>
        <tr>
            <td></td>
            <td>3</td>
            <td>广西首例！南宁一男子组织他人驾考作弊被判刑9个月</td>
            <td><a href="http://gx.sina.com.cn/zimeiti/2017-08-01/detail-ifyinvwu4273860.shtml"
                   title="http://gx.sina.com.cn/zimeiti/2017-08-01/detail-ifyinvwu4273860.shtml" target="_blank"
                   title="http://gx.sina.com.cn/zimeiti/2017-08-01/detail-ifyinvwu4273860.shtml">查看原文</a></td>
            <td>
                <button class='btn btn-xs btn-danger'>是</button>
            </td>
            <td>2017-08-01</td>
            <td>2017-11-17 09:48:11</td>
            <td>
                <a class="btn btn-xs btn-primary edituser" href="javascript:void(0);" onclick='EditNews(3)'>编辑</a>
                <a class="btn btn-xs btn-danger" href="javascript:void(0);" onclick='EditStatus(3,0)'>取消置顶</a>
                <a class="btn btn-xs btn-warning" href="javascript:void(0);" onclick='Del(3)'>删除</a>
            </td>
        </tr>
        <tr>
            <td></td>
            <td>10</td>
            <td>郫县服务中心开业</td>
            <td><a href="http://www.bmxpx.com/" title="http://www.bmxpx.com/" target="_blank"
                   title="http://www.bmxpx.com/">查看原文</a></td>
            <td>
                <button class='btn btn-xs disabled'>否</button>
            </td>
            <td>2017-12-12</td>
            <td>2017-12-12 15:38:36</td>
            <td>
                <a class="btn btn-xs btn-primary edituser" href="javascript:void(0);" onclick='EditNews(10)'>编辑</a>
                <a class="btn btn-xs btn-success" href="javascript:void(0);" onclick='EditStatus(10,1)'>设置置顶</a> <a
                    class="btn btn-xs btn-warning" href="javascript:void(0);" onclick='Del(10)'>删除</a>
            </td>
        </tr>
        <tr>
            <td></td>
            <td>5</td>
            <td>宜宾潮男戴耳机驾考 牵出背后惊天作弊团伙</td>
            <td><a href="http://sc.sina.com.cn/news/m/2017-07-01/detail-ifyhrxsk1502818.shtml"
                   title="http://sc.sina.com.cn/news/m/2017-07-01/detail-ifyhrxsk1502818.shtml" target="_blank"
                   title="http://sc.sina.com.cn/news/m/2017-07-01/detail-ifyhrxsk1502818.shtml">查看原文</a></td>
            <td>
                <button class='btn btn-xs disabled'>否</button>
            </td>
            <td>2017-12-05</td>
            <td>2017-12-05 10:35:43</td>
            <td>
                <a class="btn btn-xs btn-primary edituser" href="javascript:void(0);" onclick='EditNews(5)'>编辑</a>
                <a class="btn btn-xs btn-success" href="javascript:void(0);" onclick='EditStatus(5,1)'>设置置顶</a> <a
                    class="btn btn-xs btn-warning" href="javascript:void(0);" onclick='Del(5)'>删除</a>
            </td>
        </tr>
        <tr>
            <td></td>
            <td>4</td>
            <td>驾考作弊 自贡十被告人全部获罪</td>
            <td><a href="http://sc.sina.com.cn/news/m/2017-08-14/detail-ifyixtym3274211-p2.shtml"
                   title="http://sc.sina.com.cn/news/m/2017-08-14/detail-ifyixtym3274211-p2.shtml" target="_blank"
                   title="http://sc.sina.com.cn/news/m/2017-08-14/detail-ifyixtym3274211-p2.shtml">查看原文</a></td>
            <td>
                <button class='btn btn-xs disabled'>否</button>
            </td>
            <td>2017-08-04</td>
            <td>2017-11-17 09:49:05</td>
            <td>
                <a class="btn btn-xs btn-primary edituser" href="javascript:void(0);" onclick='EditNews(4)'>编辑</a>
                <a class="btn btn-xs btn-success" href="javascript:void(0);" onclick='EditStatus(4,1)'>设置置顶</a> <a
                    class="btn btn-xs btn-warning" href="javascript:void(0);" onclick='Del(4)'>删除</a>
            </td>
        </tr>
        </tbody>

    </table>--%>
    <table id="newstable" data-mobile-responsive="true"></table>
</div>

<script src="${pageContext.request.contextPath}/static/js/jquery.min.js"></script>
<script src="${pageContext.request.contextPath}/static/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}/static/layer/layer.js"></script>
<script src="${pageContext.request.contextPath}/static/js/plugins/bootstrap-table/bootstrap-table.js"></script>
<script src="${pageContext.request.contextPath}/static/js/plugins/bootstrap-table/bootstrap-table-mobile.js"></script>
<script src="${pageContext.request.contextPath}/static/js/plugins/bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>

<script>

    $('#newstable').bootstrapTable({
        url: '/news',
        method: 'GET',
        striped: true,
        cache: false,
        height: 630,
        pagination: true,
        sidePagination: "client",
        queryParams: function (params) { // 请求服务器数据时发送的参数，可以在这里添加额外的查询参数，返回false则终止请求

        },
        pageNumber: 1,
        pageSize: 10,
        pageList: [10, 15, 25],
        clickToSelect: true,
        uniqueId: "id",
        responseHandler: function (json) {
            console.info(json.data);
            return json.data;
        },
        columns: [
            {
                checkbox: true
            },
            {
                field:'id',
                title:'新闻ID',
            },
            {
                field:'title',
                title:'新闻标题',
            },
            {
                title:'原文地址',
                formatter:function (value, row) {
                    var s;
                    if (row.url) {
                        var url = row.url;
                        s = "<a href="+url+" title="+url+" target='_blank'>查看原文</a>";
                    }
                    return s;
                }
            },
            {
                title:'置顶',
                formatter:function (value, row) {
                    if (row.zt){
                        return "<button class=\"btn btn-xs btn-danger\">是</button>";
                    }else{
                        return "<button class=\"btn btn-xs disabled\">否</button>";
                    }
                    return false;
                }
            },
            {
                field:'stime',
                title:'发布时间',
                formatter: function (value) {
                    if (value === null) {
                        return "-";
                    } else {
                        var crtTime = new Date(parseInt(value) * 1000);
                        return timeformat("yyyy-MM-dd", crtTime);
                    }
                }
            },
            {
                field:'ctime',
                title:'创建时间',
                formatter: function (value) {
                    if (value === null) {
                        return "-";
                    } else {
                        var crtTime = new Date(parseInt(value) * 1000);
                        return timeformat("yyyy-MM-dd hh:mm:ss", crtTime);
                    }
                }
            },
            {
                title:'操作',
                formatter:function (value, row) {
                    if (row.zt){
                        return "<a class=\"btn btn-xs btn-primary edituser\" href=\"javascript:void(0);\" onclick=\"EditNews("+row.id+")\">编辑</a> \n" +
                            "<a class=\"btn btn-xs btn-danger\" href=\"javascript:void(0);\" onclick=\"EditStatus("+row.id+",0)\">取消置顶</a> \n" +
                            "<a class=\"btn btn-xs btn-warning\" href=\"javascript:void(0);\" onclick=\"Delnew("+row.id+")\">删除</a>"
                    }else {
                        return "<a class=\"btn btn-xs btn-primary edituser\" href=\"javascript:void(0);\" onclick=\"EditNews("+row.id+")\">编辑</a> \n" +
                            "<a class=\"btn btn-xs btn-success\" href=\"javascript:void(0);\" onclick=\"EditStatus("+row.id+",1)\">设置置顶</a> \n" +
                            "<a class=\"btn btn-xs btn-warning\" href=\"javascript:void(0);\" onclick=\"Delnew("+row.id+")\">删除</a>"
                    }

                    return false;
                }
            }]
    });

    //格式化时间
    function timeformat(fmt, date) {
        var o = {
            "M+": date.getMonth() + 1,                 //月份
            "d+": date.getDate(),                    //日
            "h+": date.getHours(),                   //小时
            "m+": date.getMinutes(),                 //分
            "s+": date.getSeconds(),                 //秒
            "S": date.getMilliseconds()             //毫秒
        };
        if (/(y+)/.test(fmt))
            fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }

    //添加新闻
    function AddNews() {
        layer.open({
            type: 2,
            title: '新增新闻',
            area: ['800px', '100%'],
            fixed: false,
            maxmin: true,
            shadeClose: true,
            content: "/page/webmanage/add_news.jsp",
            success:function () {
                var body = layer.getChildFrame('body', index);
                var ctime = Date.parse(new Date())/1000;
                body.find("input[name='ctime']").val(ctime);
            }
        });
    }

    //设置置顶
    function EditStatus(id,zt) {
        var ajax1= {
            type: 'put',
            url: "/news",
            data: {id: id, zt: zt},
            dataType: 'json',
            success: function (json) {
                if (json.status === "success") {
                    layer.msg('操作成功', {icon: 1, time: 1000});
                    $('#newstable').bootstrapTable('refresh');
                } else {
                    layer.msg(json.status, {icon: 2, time: 1000});
                }
            },
            error: function () {
                layer.msg('服务器繁忙，请刷新页面后重试', {icon: 5, time: 1000});
            }
        };
        if(!zt){
            layer.confirm('确定是否置顶', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                $.ajax(ajax1);
            }, function () {
                layer.msg('取消操作成功', {icon: 1, time: 1000});
            });
        }else{
            layer.confirm('确定取消置顶', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                $.ajax(ajax1);
            }, function () {
                layer.msg('取消操作成功', {icon: 1, time: 1000});
            });
        }
    }

    //编辑新闻
    function EditNews(id) {
        var aa = $('#newstable').bootstrapTable('getRowByUniqueId', id);
        console.info(aa);
        layer.open({
            type: 2,
            title: '编辑新闻',
            area: ['800px', '90%'],
            fixed: false, //不固定
            maxmin: true,
            content: "/page/webmanage/edit_news.jsp?id="+id,
            success: function (layero, index) {
                var body = layer.getChildFrame('body', index);
                body.find("input[name='id']").val(aa.id);
                body.find("input[name='title']").val(aa.title);
                body.find("input[name='url']").val(aa.url);
                if (!aa.stime) {
                    return "-";
                } else {
                    var crtTime = new Date(parseInt(aa.stime) * 1000);
                    var fabutime = timeformat("yyyy-MM-dd", crtTime);
                    body.find("input[name='stime']").val(fabutime);
                }
                var iframeWin = window[layero.find('iframe')[0]['name']];
                iframeWin.Contentfn(aa.content);

            }
        });
    }

    //删除单条新闻
    function Delnew(key) {
        var ids = [];
        ids.push(key);
        layer.confirm('确定是否删除该题所有信息', {
            btn: ['确定', '取消'] //按钮
        }, function () {
            $.ajax({
                contentType: 'application/json;charset=UTF-8',
                type: 'DELETE',
                dataType: "json",
                data: JSON.stringify(ids),
                url :'/news',
                success: function (json) {
                    if (json.status=="success") {
                        layer.msg('操作成功', {icon: 1, time: 1000});
                        $('#newstable').bootstrapTable('refresh');
                    } else {
                        layer.msg('删除失败', {icon: 2, time: 1000});
                    }
                },
                error: function () {
                    layer.msg('网络连接失败', {icon: 5, time: 1000});
                }
            });
        }, function () {
            layer.msg('取消操作成功', {icon: 1, time: 1000});
        });
    }

    function Delnews() {
        var ids = $.map($("#newstable").bootstrapTable('getSelections'), function (row) {
            return row.id;
        });
        if (ids.length == 0) {
            layer.msg('请选择数据');
        } else {
            layer.confirm('确定是否删除选中题目', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                $.ajax({
                    contentType: 'application/json;charset=UTF-8',
                    type: 'delete',
                    url: "/news",
                    data: JSON.stringify(ids),
                    dataType: 'json',
                    success: function (json) {
                        if (json.status == "success") {
                            layer.msg('操作成功', {icon: 1, time: 1000});
                            $('#newstable').bootstrapTable('refresh');
                        } else {
                            layer.msg('删除失败', {icon: 2, time: 1000});
                        }
                    },
                    error: function () {
                        layer.msg('服务器繁忙，请刷新页面后重试', {icon: 5, time: 1000});
                    }
                });
            }, function () {
                layer.msg('取消操作成功', {icon: 1, time: 1000});
            });
        }
    }

    //格式化时间
    function timeformat(fmt, date) {
        var o = {
            "M+": date.getMonth() + 1,                 //月份
            "d+": date.getDate(),                    //日
            "h+": date.getHours(),                   //小时
            "m+": date.getMinutes(),                 //分
            "s+": date.getSeconds(),                 //秒
            "S": date.getMilliseconds()             //毫秒
        };
        if (/(y+)/.test(fmt))
            fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }

    //关键字或ID搜索新闻
    function Searchnews() {
        var value = $("input[name='keywords']").val();
        $('#newstable').bootstrapTable('refresh', {url: '/seekNews?keyCode='+value});
    }




</script>

</body>

</html>
